package com.feri.rbacsys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feri.rbacsys.bo.CustomAddBo;
import com.feri.rbacsys.bo.CustomBo;
import com.feri.rbacsys.dao.CustomAddressMapper;
import com.feri.rbacsys.dao.CustomMapper;
import com.feri.rbacsys.entity.Custom;
import com.feri.rbacsys.entity.CustomAddress;
import com.feri.rbacsys.service.intf.CustomService;
import com.feri.rbacsys.vo.R;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
* @author wmg
* @description 针对表【t_custom(客户表)】的数据库操作Service实现
* @createDate 2023-03-28 08:45:26
*/
@Service
public class CustomServiceImpl implements CustomService{

    @Resource
    private CustomMapper dao;
    @Resource
    private CustomAddressMapper adDao;
    @Transactional
    @Override
    public R addCustom(CustomAddBo bo) {
        // 校验
        if(bo!=null){
            // 组装对象
            Custom custom = new Custom();
            BeanUtils.copyProperties(bo,custom);
            // 是否有客户编号，没有自动生成
            boolean hasCno = StringUtils.hasLength(custom.getCno());
            custom.init(hasCno);
            // 新增客户
            int result1=dao.insert(custom);
            // 新增地址
            // 遍历地址列表，一一存放到address表中
            for (CustomAddress ad: bo.getAddress()) {
                CustomAddress customAddress = new CustomAddress();
                // 获取地址信息
                BeanUtils.copyProperties(ad,customAddress);
                // 存储cno 新增客户的编号 和 创建时间
                customAddress.init(custom.getCno());
                adDao.insert(customAddress);
            }
            if (result1>0){
                return R.ok();
            }
        }
        return R.fail("添加客户异常！");
    }

    @Override
    public R queryByCno(String cno) {
        if(StringUtils.hasLength(cno)){
            Custom custom = dao.selectOne(new QueryWrapper<Custom>().eq("cno", cno));
            List<CustomAddress> address = adDao.selectList(new QueryWrapper<CustomAddress>().eq("cno", cno));
            CustomAddBo customAddBo = new CustomAddBo();
            BeanUtils.copyProperties(custom,customAddBo);
            customAddBo.setAddress(address);
            return R.ok(customAddBo);
        }
        return R.fail("查询客户编号异常！");
    }

    @Override
    public R queryAll(CustomBo bo, Integer page, Integer size) {
        // 为page和size设置初始值
        if (page==null){
            page = 1;
        }
        if (size==null){
            size=10;
        }
        QueryWrapper<Custom> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(bo.getName())){
            queryWrapper.like("name",bo.getName());
        }
        if (StringUtils.hasLength(bo.getPhone())){
            queryWrapper.eq("phone",bo.getPhone());
        }
        if (bo.getSources()!=0){
            queryWrapper.eq("sources",bo.getSources());
        }
        if (bo.getLevel()!=0){
            queryWrapper.eq("level",bo.getLevel());
        }
        if (bo.getTag()!=0){
            queryWrapper.eq("tag",bo.getTag());
        }
        Page<Custom> customPage = dao.selectPage(new Page<>(page, size), queryWrapper);
        return R.ok(customPage);
    }


}




